package com.skateboard.reelviewtest.rotate

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.os.Bundle
import android.view.View
import android.view.animation.LinearInterpolator
import androidx.appcompat.app.AppCompatActivity
import com.skateboard.reelviewtest.R
import kotlinx.android.synthetic.main.activity_rotation.*

class RotationActivity : AppCompatActivity(), View.OnClickListener {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_rotation)
        tv_1.setOnClickListener(this)
        tv_2.setOnClickListener(this)
    }

    private fun startAnimation() {

        val objectAnimator1 = ObjectAnimator.ofFloat(layout_1, "scaleX", 1f, 2f)


        val objectAnimator2 = ObjectAnimator.ofFloat(layout_1, "scaleY", 1f, 2f)
        val set = AnimatorSet()

        set.play(objectAnimator1).with(objectAnimator2)
        set.duration = 2000


        var centerX = layout_1.width / 2
        var centerY = layout_1.height / 2
        var rotateAnimation1 = Rotate3dAnimation(this, 0f, 360f, centerX.toFloat(), centerY.toFloat(), 1f, true)
        rotateAnimation1.duration = 2000
        rotateAnimation1.fillAfter = true
        rotateAnimation1.interpolator = LinearInterpolator()
        layout_1.startAnimation(rotateAnimation1)
        set.start()


    }

    private fun startAnimation2() {
        var rotationAnimator=ObjectAnimator.ofFloat(layout_1, "rotationY", 0f, 360f) //绕X轴翻转
        val objectAnimator1 = ObjectAnimator.ofFloat(layout_1, "scaleX", 1f, 2f)
        val objectAnimator2 = ObjectAnimator.ofFloat(layout_1, "scaleY", 1f, 2f)
        val set = AnimatorSet()
        set.play(objectAnimator1).with(objectAnimator2).with(rotationAnimator)
        set.duration = 2000

        set.start()


    }

    override fun onClick(v: View?) {
        when(v?.id){
            R.id.tv_1->{
                startAnimation()
            }
            R.id.tv_2->{
                startAnimation2()
            }
        }

    }
}